package XiaoMi;

/**
 * @author leiwenkang <leiwenkang@kuaishou.com>
 * Created on 2022-07-08
 */
public class Test交替打印 {

    public static void main(String[] args) {
        final Object o = new Object();

        new Thread(() -> {
            synchronized (o) {
                for (int i = 1; i <= 100; i++) {
                    if ((i & 1) == 1) {
                        System.out.print(i);
                    }
                    try {
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify();
            }
        }, "t1").start();


        new Thread(() -> {
            synchronized (o) {
                for (int i = 1; i <= 100; i++) {
                    if ((i & 1) == 0) {
                        System.out.print(i);
                    }
                    try {
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify();
            }
        }, "t2").start();



    }
}
